package arithmetic;

/**
 * 冒泡排序法：
 * @author: yyfei  
 * @email:  yyfei123@qq.com
 */
public class ArrayMaoPao {
	public static void main(String[] args) {
		//定义数组
		int[] arr=new int[] {45,78,9,6,45,2,4,85,6,8,52,98,100,45,65,87};
		//遍历数组
		System.out.print("排序前：");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+",");
		}
		
		
		//冒泡排序； 循环次数当循环到最后一位时，不用再循环了，所以减1
		//控制循环的次数
		for (int i = 0; i < arr.length-1; i++) {
			//对数据进行比较
			for (int j = 0; j < arr.length-1-i; j++) {
				if(arr[j]>arr[j+1]) {
					int temp=arr[j];
					arr[j]=arr[j+1];
					arr[j+1]=temp;
				}
			}
		}
		
		
		System.out.println("\n");
		//遍历数组
		System.out.print("排序后：");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+",");
		}
	}
}
